package run;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import processing.core.PApplet;
import processing.core.PVector;

public class Env {
	private static final int NUM_CELLS = 10;
	
	List<LineCell> cells = new ArrayList<LineCell>();
	List<LineCell> cellQ = new ArrayList<LineCell>();
	
	LineCell[][] occ = new LineCell[900][500];
	
	private float xdim = 900;
	private float ydim = 500;
	
	PApplet p;
	
	public Env (PApplet p) {
		this.p = p;
		for (int i = 0; i < NUM_CELLS; i++ )
			cells.add(new LineCell(
						new PVector(p.random(xdim), p.random(ydim),0),
						new PVector(p.random(-1,1), p.random(-1,1),0),
						p,
						0));
	}

	public void draw() {
		cells.addAll(cellQ);
		cellQ.clear();
		for (LineCell c : cells) {
			c.move(this);
		}
	}
	
	void addCell(LineCell c) {
		
		if (cellQ.size() < 50)
			cellQ.add(c);
	}
	
	// if we struck another linecell, report which cell was hit.
	public LineCell isCollide(LineCell o) {
		PVector loc = o.loc;
		
		LineCell c = occ[(int)loc.x][(int)loc.y];
		
		// scan a fuzzy range of 5 px
		
		if (c != o)
			return c;
		return null;
	}
	
	public void addHist(LineCell c, PVector loc) {
		occ[(int)loc.x][(int)loc.y] = c;
	}

}
